package com.avic.rong.wedgit.customView;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.avic.rong.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/7/15 20:22
 * E-Mail Address：16601203965@163.com
 */
public class SearchGroupAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ItemSearchBean> dataList = new ArrayList<>();
    public static final int TYPE_SELECT = 1;//选择
    public static final int TYPE_TIME = 6;//时间
    public static final int TYPE_WORD = 2;//文本
    private boolean clearData = false;
    public Map<Integer,SearchItemView> searchItemViewMap = new HashMap<>();


    public void setDataList(List<ItemSearchBean> dataList) {
        this.dataList = dataList;
    }

    public SearchGroupAdapter(Context context) {
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int type) {
        View view = null;
        if (type == TYPE_TIME || type == TYPE_SELECT) {
            view = LayoutInflater.from(context).inflate(R.layout.item_search_group_time, viewGroup, false);
            return new GroupTimeViewHolder(view);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.item_search_group_word, viewGroup, false);
            return new GroupWordViewHolder(view);
        }


    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {

        ItemSearchBean searchBean = dataList.get(position);
        if (getItemViewType(position) == TYPE_SELECT || getItemViewType(position) == TYPE_TIME) {
            GroupTimeViewHolder timeViewHolder = (GroupTimeViewHolder) holder;
            timeViewHolder.search_item.setSearchBeanList(searchBean, clearData);

            if (searchItemViewMap.get(searchBean.getId()) == null){//保存SearchItemView 暴露给外面使用book popwindow
                searchItemViewMap.put(searchBean.getId(), timeViewHolder.search_item);
            }
        } else {
            GroupWordViewHolder wordViewHolder = (GroupWordViewHolder) holder;
            wordViewHolder.tv_name.setText(searchBean.getName());
            wordViewHolder.edit_value.setText(searchBean.getValue());
            wordViewHolder.edit_value.addTextChangedListener(new MyTextWatcher(searchBean));
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return dataList.get(position).getControlType();
    }


    public void clearData() {
        clearData = true;
        notifyDataSetChanged();
    }



    class MyTextWatcher implements TextWatcher {

        ItemSearchBean searchBean;

        public MyTextWatcher(ItemSearchBean searchBean) {
            this.searchBean = searchBean;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String currentString = s.toString();
            if (!TextUtils.isEmpty(currentString)) {
                currentString = currentString.trim();
            }
            searchBean.setValue(currentString);
        }
    }


    class GroupTimeViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.search_item)
        SearchItemView search_item;

        public GroupTimeViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class GroupWordViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tv_name;
        @BindView(R.id.edit_value)
        EditText edit_value;

        public GroupWordViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
